﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ImpExpData.Interface;
using ImpExpData.Presenter;

namespace ImpExpData
{
    public partial class ImportForm : Form, IImportForm
    {
        #region ISettingsForm Members
        public bool IsAllGoods
        {
            get
            {
                return allGoodsCheckBox.Checked;
            } 
            set
            {
                allGoodsCheckBox.Checked = value;
            }
        }

        public bool IsRemaining
        {
            get
            {
                return remainingGoodsCheckBox.Checked;
            } 
            set
            {
                remainingGoodsCheckBox.Checked = value;
            }
        }
        public bool IsBuyers
        {
            get
            {
                return buyersCheckBox.Checked;
            } 
            set
            {
                buyersCheckBox.Checked = value;
            }
        }
        public bool IsDebts
        {
            get
            {
                return debtCheckBox.Checked;
            } 
            set
            {
                debtCheckBox.Checked = value;
            }
        }

        public void EndData()
        {
            MessageBox.Show("Дані успішно завантажені");
            loadButton.Text = "Завантажити";
            loadButton.Tag = "load";
        }

        public void ErrorData()
        {
            MessageBox.Show("Загрузка перервана", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            loadButton.Text = "Завантажити";
            loadButton.Tag = "load";
        }

        public void SetStatusLabel (string caption)
        {
            statusLabel.Text = caption;
        }

        public void SetStatusPosition (int position)
        {
            statusProgressBar.Value = position;
        }

        public void SetStatusMax(int max)
        {
            statusProgressBar.Maximum = max;
        }

        public event EventHandler<EventArgs> StartImportData;
        public event EventHandler<EventArgs> AbortImportData;
        #endregion
       
        public ImportForm()
        {
            InitializeComponent();
            new ImportPresenter(this);
        }

        private void loadButton_Click(object sender, EventArgs e)
        {
            if ((string) ((Button)sender).Tag == "load")
            {
                int start = Environment.TickCount;
                DateTime d1 = DateTime.Now;
                StartImportData(null, null);
                ((Button) sender).Tag = "abort";
                ((Button)sender).Text = "Перервати";
                int end = Environment.TickCount;
                DateTime d2 = DateTime.Now;
                MessageBox.Show((end - start).ToString() + " - " + (d2 - d1).Seconds);
            }
            else if ((string)((Button)sender).Tag == "abort")
            {
                AbortImportData(null, null);
                ((Button)sender).Tag = "load";
                loadButton.Text = "Завантажити";
            }
        }

        private void allGoodsCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            remainingGoodsCheckBox.Checked = allGoodsCheckBox.Checked;
        }
    }
}